package com.icow.wms.basic.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

import com.icow.wms.basic.domain.BarChart;
import com.icow.wms.basic.domain.LineChart;
import com.icow.wms.basic.domain.PieChart;
import com.icow.wms.basic.utils.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icow.wms.basic.mapper.AgvTaskMapper;
import com.icow.wms.basic.domain.AgvTask;
import com.icow.wms.basic.service.IAgvTaskService;

/**
 * taskService业务层处理
 *
 * @author lqq
 * @date 2024-08-06
 */
@Service
@Slf4j
public class AgvTaskServiceImpl implements IAgvTaskService {
    @Autowired
    private AgvTaskMapper agvTaskMapper;

    /**
     * 查询task
     *
     * @param id taskID
     * @return task
     */
    @Override
    public AgvTask selectAgvTaskById(Long id) {
        return agvTaskMapper.selectAgvTaskById(id);
    }

    @Override
    public PieChart selectForPieChart(String orderState) {
        AgvTask agvTask = new AgvTask();
        agvTask.setStatus(orderState);
        List<AgvTask> agvTasks = agvTaskMapper.selectAgvTaskList(agvTask);
        return new PieChart(agvTasks.size(), orderState);
    }


    @Override
    public LineChart selectForLineChart(Integer day) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        AgvTask agvTask = new AgvTask();
        Date startTime = timeFormat.parse(dateFormat.format(new Date()) + " 00:00:00");
        startTime = new Date().from(startTime.toInstant().minus(day, ChronoUnit.DAYS));
        agvTask.setStartTime(startTime);

        Date endTime = timeFormat.parse(dateFormat.format(new Date()) + " 23:59:59");
        endTime = new Date().from(endTime.toInstant().minus(day, ChronoUnit.DAYS));
        agvTask.setEndTime(endTime);
        List<AgvTask> agvTasks = agvTaskMapper.selectAgvTaskByDay(agvTask);
        String[] split = dateFormat.format(startTime).split("-");
        LineChart lineChart = new LineChart(split[1] + "-" + split[2], agvTasks.size());
        return lineChart;
    }

    @Override
    public BarChart selectForBarChart(Integer day) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        AgvTask agvTask = new AgvTask();
        Date startTime = timeFormat.parse(dateFormat.format(new Date()) + " 00:00:00");
        startTime = new Date().from(startTime.toInstant().minus(day, ChronoUnit.DAYS));
        agvTask.setStartTime(startTime);

        Date endTime = timeFormat.parse(dateFormat.format(new Date()) + " 23:59:59");
        endTime = new Date().from(endTime.toInstant().minus(day, ChronoUnit.DAYS));
        agvTask.setEndTime(endTime);
        List<AgvTask> agvTasks = agvTaskMapper.selectAgvTaskByDay(agvTask);
        String[] split = dateFormat.format(startTime).split("-");
        BarChart barChart = new BarChart();
        barChart.setDate(split[1] + "-" + split[2]);
        barChart.setAllNumber(agvTasks.size());
        agvTask.setStatus(OrderState.ORDER_STATE_FAIL);
        barChart.setFailedNumber(agvTaskMapper.selectAgvTaskByDay(agvTask).size());
        agvTask.setStatus(OrderState.ORDER_STATE_INIT);
        barChart.setUnexecutedNumber(agvTaskMapper.selectAgvTaskByDay(agvTask).size());
        agvTask.setStatus(OrderState.ORDER_STATE_EXECUTING);
        barChart.setExecutedNumber(agvTaskMapper.selectAgvTaskByDay(agvTask).size());
        agvTask.setStatus(OrderState.ORDER_STATE_LOAD_FINISH);
        barChart.setLoadFinishNumber(agvTaskMapper.selectAgvTaskByDay(agvTask).size());
        agvTask.setStatus(OrderState.ORDER_STATE_FINISH);
        barChart.setSuccessedNumber(agvTaskMapper.selectAgvTaskByDay(agvTask).size());
        barChart.setAllNumber(agvTasks.size());
        return barChart;
    }


    /**
     * 查询task列表
     *
     * @param agvTask task
     * @return task
     */
    @Override
    public List<AgvTask> selectAgvTaskList(AgvTask agvTask) {
        return agvTaskMapper.selectAgvTaskList(agvTask);
    }

    /**
     * 新增task
     *
     * @param agvTask task
     * @return 结果
     */
    @Override
    public int insertAgvTask(AgvTask agvTask) {
        return agvTaskMapper.insertAgvTask(agvTask);
    }

    /**
     * 修改task
     *
     * @param agvTask task
     * @return 结果
     */
    @Override
    public int updateAgvTask(AgvTask agvTask) {
        return agvTaskMapper.updateAgvTask(agvTask);
    }

    /**
     * 批量删除task
     *
     * @param ids 需要删除的taskID
     * @return 结果
     */
    @Override
    public int deleteAgvTaskByIds(Long[] ids) {
        return agvTaskMapper.deleteAgvTaskByIds(ids);
    }

    /**
     * 删除task信息
     *
     * @param id taskID
     * @return 结果
     */
    @Override
    public int deleteAgvTaskById(Long id) {
        return agvTaskMapper.deleteAgvTaskById(id);
    }

    @Override
    public int deleteThirtyDaysAgoData() {
        return agvTaskMapper.deleteThirtyDaysAgoData();
    }

    public void setOrderState(String orderId, String orderState, String vehicleName) {
        if (OrderState.ORDER_STATE_EXECUTING.equals(orderState)) {
            AgvTask agvTask = new AgvTask();
            agvTask.setInnerTaskName(orderId);
            agvTask.setStatus(orderState);
            agvTask.setAgvName(vehicleName);
            agvTaskMapper.updateAgvTask(agvTask);
        } else if (OrderState.ORDER_STATE_LOAD_FINISH.equals(orderState)) {
            AgvTask agvTask = new AgvTask();
            agvTask.setInnerTaskName(orderId);
            agvTask.setStatus(orderState);
            agvTaskMapper.updateAgvTask(agvTask);
        } else if (OrderState.ORDER_STATE_UNLOAD_FINISH.equals(orderState)) {
            AgvTask agvTask = new AgvTask();
            agvTask.setInnerTaskName(orderId);
            agvTask.setStatus(orderState);
            agvTaskMapper.updateAgvTask(agvTask);
        } else if (OrderState.ORDER_STATE_FINISH.equals(orderState) || OrderState.ORDER_STATE_FAIL.equals(orderState)) {
            AgvTask agvTask = new AgvTask();
            agvTask.setInnerTaskName(orderId);
            agvTask.setStatus(orderState);
            agvTask.setEndTime(new Date());
            agvTaskMapper.updateAgvTask(agvTask);
        } else {
            log.info(orderState + " 无效");
        }
    }

}
